
#include "async_io.h"

#define BUFFER_SIZE 1024

// 定义一个宏用于表示未使用的参数
#define UNUSED(x) (void)(x)

#include <stdio.h>
#include <unistd.h>

char   buff[BUFFER_SIZE];
size_t size_buff = 0;

static void app_destroy()
{
    // 关闭套接字
    async_exit();
}

static int on_write(int fd, int write_size)
{
    UNUSED(fd);
    puts("on_write:");
    if (write_size > 0) {
        puts("write sucses");
    }
    return 0;
}

static int on_read(int fd, int read_size, void *data)
{
    UNUSED(fd);
    puts("on_read:");
    if (read_size > 0) {
        if (strncmp(data, "exit", 4) == 0) {
            async_exit();
            return 0;
        }
        async_write(STDOUT_FILENO, data, read_size, on_write);
    }
    async_read(STDIN_FILENO, buff, BUFFER_SIZE, on_read);
    return 0;
}

int main()
{
    atexit(app_destroy);

    async_init();
    async_read(STDIN_FILENO, buff, BUFFER_SIZE, on_read);
    async_exec();

    return 0;
}
